package com.example.demo.dto;


import com.example.demo.common.enums.EnumResultCode;
import lombok.Data;
import org.slf4j.MDC;

/**
 * @Description: < 接口返回数据统一模板 >
 * @Author: WangZhiBo
 * @Date: 2020/12/17 11:58 上午
 */
@Data
public class ResponseResult<T> {

    public static final int ERROR_CODE = 1;

    /**
     * 返回状态 0：成功 1：失败
     */
    private int status;

    /**
     * 返回状态码
     */
    private int code;

    /**
     * 返回提示信息
     */
    private String msg;

    /**
     * 链路日志traceId
     */
    private String traceId;

    /**
     * 返回数据
     */
    private T data;

    private ResponseResult(){
        this.status = EnumResultCode.SUCCESS.getStatus();
        this.code = EnumResultCode.SUCCESS.getCode();
        this.msg = EnumResultCode.SUCCESS.getMessage();
        this.traceId = MDC.get("traceId");
    }

    private ResponseResult(int status, int code, String msg, String traceId){
        this.status = status;
        this.code = code;
        this.msg = msg;
        this.traceId = traceId;
    }

    private ResponseResult(int status, int code, String msg, T data, String traceId) {
        this(status,code,msg,traceId);
        this.data = data;
    }

    private ResponseResult(EnumResultCode enumResultCode){
        this(enumResultCode.getStatus(),enumResultCode.getCode(),enumResultCode.getMessage(), MDC.get("traceId"));
    }

    private ResponseResult(EnumResultCode enumResultCode, T data){
        this(enumResultCode);
        this.data = data;
    }

    public static <T> ResponseResult<T> build(EnumResultCode enumResultCode) {
        return new ResponseResult<>(enumResultCode);
    }

    public static <T> ResponseResult<T> build(int status, int code, String msg, String traceId) {
        return new ResponseResult<>(status, code, msg, traceId);
    }

    public static <T> ResponseResult<T> build(EnumResultCode resultCode, String msg, String traceId) {
        return new ResponseResult<>(resultCode.getStatus(), resultCode.getCode(), msg, traceId);
    }

    public static <T> ResponseResult<T> build(int status, int code, String msg, T data, String traceId) {
        return new ResponseResult<>(status, code, msg, data, traceId);
    }


    public static <T> ResponseResult buildSuccess() {
        return new ResponseResult(EnumResultCode.SUCCESS, null);
    }

    public static <T> ResponseResult buildError() {
        return new ResponseResult(EnumResultCode.ERROR);
    }

    public static <T> ResponseResult buildSuccess(T data) {
        return new ResponseResult(EnumResultCode.SUCCESS, data);
    }

    public static <T> ResponseResult buildError(EnumResultCode enumResultCode) {
        return new ResponseResult(enumResultCode);
    }

    public static <T> ResponseResult buildError(String msg) {
        return new ResponseResult(EnumResultCode.ERROR.getStatus(),EnumResultCode.ERROR.getCode(), msg, MDC.get("traceId"));
    }

    public static <T> ResponseResult buildError(Integer errCode, String msg) {
        return new ResponseResult(errCode, errCode, msg, MDC.get("traceId"));
    }
}
